VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
Object = "{B3FB64BF-91F9-11D7-A482-0008A14158BC}#2.22#0"; "itgcontrols.ocx"
Begin VB.Form frmARDRList 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "DR Detail List"
   ClientHeight    =   5340
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   8565
   BeginProperty Font 
      Name            =   "Tahoma"
      Size            =   8.25
      Charset         =   0
      Weight          =   400
      Underline       =   0   'False
      Italic          =   0   'False
      Strikethrough   =   0   'False
   EndProperty
   Icon            =   "frmARDRList.frx":0000
   KeyPreview      =   -1  'True
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   5340
   ScaleWidth      =   8565
   StartUpPosition =   1  'CenterOwner
   Begin VB.Frame Frame1 
      Height          =   2205
      Left            =   60
      TabIndex        =   11
      Top             =   60
      Width           =   8430
      Begin MSComctlLib.ListView lvwRef 
         Height          =   1845
         Left            =   105
         TabIndex        =   4
         Top             =   225
         Width           =   1635
         _ExtentX        =   2884
         _ExtentY        =   3254
         View            =   3
         LabelWrap       =   -1  'True
         HideSelection   =   -1  'True
         FullRowSelect   =   -1  'True
         GridLines       =   -1  'True
         HotTracking     =   -1  'True
         _Version        =   393217
         ForeColor       =   -2147483640
         BackColor       =   -2147483643
         BorderStyle     =   1
         Appearance      =   0
         NumItems        =   1
         BeginProperty ColumnHeader(1) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
            Text            =   "Reference No."
            Object.Width           =   2646
         EndProperty
      End
      Begin ITGControls.ITGTextBox txtCode 
         Height          =   285
         Left            =   2040
         TabIndex        =   0
         Top             =   240
         Width           =   2265
         _ExtentX        =   3784
         _ExtentY        =   503
         SendKeysTab     =   -1  'True
         BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
            Name            =   "Tahoma"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         AllCaps         =   -1  'True
         Mandatory       =   -1  'True
         Locked          =   -1  'True
         Label           =   "Customer"
         BeginProperty LabelFont {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
            Name            =   "Tahoma"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         LabelWidth      =   1000
         TextBoxWidth    =   1205
      End
      Begin ITGControls.ITGTextBox txtAddress 
         Height          =   285
         Left            =   2040
         TabIndex        =   2
         Top             =   600
         Width           =   6255
         _ExtentX        =   10821
         _ExtentY        =   503
         SendKeysTab     =   -1  'True
         BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
            Name            =   "Tahoma"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Locked          =   -1  'True
         Label           =   "Address"
         BeginProperty LabelFont {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
            Name            =   "Tahoma"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         LabelWidth      =   1000
         TextBoxWidth    =   5195
      End
      Begin ITGControls.ITGDateBox dtbDate 
         Height          =   285
         Left            =   3075
         TabIndex        =   3
         Tag             =   "Order Date"
         Top             =   960
         Width           =   1245
         _ExtentX        =   2196
         _ExtentY        =   503
         Enabled         =   0   'False
         BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
            Name            =   "Tahoma"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Text            =   ""
         SendKeysTab     =   -1  'True
         Mandatory       =   -1  'True
         DatePicker      =   0   'False
      End
      Begin ITGControls.ITGLabel lblDate 
         Height          =   285
         Left            =   2085
         TabIndex        =   12
         Top             =   960
         Width           =   915
         _ExtentX        =   1614
         _ExtentY        =   503
         Caption         =   "Ref. Date"
         BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
            Name            =   "Tahoma"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
      End
      Begin ITGControls.ITGTextBox txtName 
         Height          =   285
         Left            =   4365
         TabIndex        =   1
         Top             =   240
         Width           =   3930
         _ExtentX        =   6826
         _ExtentY        =   503
         SendKeysTab     =   -1  'True
         BackColor       =   14737632
         BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
            Name            =   "Tahoma"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Locked          =   -1  'True
         Label           =   "ITGtext"
         BeginProperty LabelFont {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
            Name            =   "Tahoma"
            Size            =   8.25
            Charset         =   0
            Weight          =   400
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         LabelWidth      =   0
         TextBoxWidth    =   3870
      End
      Begin ITGControls.ITGLabel lblFOC 
         Height          =   285
         Left            =   2025
         TabIndex        =   13
         TabStop         =   0   'False
         Top             =   1785
         Visible         =   0   'False
         Width           =   6255
         _ExtentX        =   11033
         _ExtentY        =   503
         BackColor       =   0
         ForeColor       =   12648447
         Caption         =   "FOC ALERT! (Bold/Red colored product ID indicates FOC products)"
         BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
            Name            =   "Tahoma"
            Size            =   8.25
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Alignment       =   2
      End
   End
   Begin VB.Frame Frame4 
      Caption         =   "Product Listing"
      Height          =   2505
      Left            =   60
      TabIndex        =   10
      Top             =   2325
      Width           =   8430
      Begin MSComctlLib.ListView lvwList 
         Height          =   2130
         Left            =   120
         TabIndex        =   5
         Top             =   255
         Width           =   8190
         _ExtentX        =   14446
         _ExtentY        =   3757
         View            =   3
         LabelWrap       =   -1  'True
         HideSelection   =   -1  'True
         Checkboxes      =   -1  'True
         FullRowSelect   =   -1  'True
         GridLines       =   -1  'True
         HotTracking     =   -1  'True
         _Version        =   393217
         ForeColor       =   -2147483640
         BackColor       =   -2147483643
         BorderStyle     =   1
         Appearance      =   0
         NumItems        =   10
         BeginProperty ColumnHeader(1) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
            Text            =   "Product ID"
            Object.Width           =   2646
         EndProperty
         BeginProperty ColumnHeader(2) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
            SubItemIndex    =   1
            Text            =   "Description"
            Object.Width           =   3528
         EndProperty
         BeginProperty ColumnHeader(3) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
            SubItemIndex    =   2
            Text            =   "Warehouse"
            Object.Width           =   1764
         EndProperty
         BeginProperty ColumnHeader(4) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
            SubItemIndex    =   3
            Text            =   "Unit"
            Object.Width           =   1102
         EndProperty
         BeginProperty ColumnHeader(5) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
            Alignment       =   1
            SubItemIndex    =   4
            Text            =   "Quantity"
            Object.Width           =   1587
         EndProperty
         BeginProperty ColumnHeader(6) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
            Alignment       =   1
            SubItemIndex    =   5
            Text            =   "Price"
            Object.Width           =   1587
         EndProperty
         BeginProperty ColumnHeader(7) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
            Alignment       =   1
            SubItemIndex    =   6
            Text            =   "Amount"
            Object.Width           =   1799
         EndProperty
         BeginProperty ColumnHeader(8) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
            SubItemIndex    =   7
            Text            =   "Reference Identity"
            Object.Width           =   0
         EndProperty
         BeginProperty ColumnHeader(9) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
            SubItemIndex    =   8
            Text            =   "Factor"
            Object.Width           =   0
         EndProperty
         BeginProperty ColumnHeader(10) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
            SubItemIndex    =   9
            Text            =   "Barcode ID"
            Object.Width           =   2540
         EndProperty
      End
   End
   Begin ITGControls.ITGCommandButton cmdCancel 
      Height          =   345
      Left            =   7290
      TabIndex        =   9
      Top             =   4920
      Width           =   1200
      _ExtentX        =   2117
      _ExtentY        =   609
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "Tahoma"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Caption         =   "&Cancel"
   End
   Begin ITGControls.ITGCommandButton cmdOK 
      Height          =   345
      Left            =   6030
      TabIndex        =   8
      Top             =   4920
      Width           =   1200
      _ExtentX        =   2117
      _ExtentY        =   609
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "Tahoma"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Caption         =   "&OK"
   End
   Begin ITGControls.ITGCommandButton cmdCAll 
      Height          =   345
      Left            =   75
      TabIndex        =   6
      Top             =   4920
      Width           =   1200
      _ExtentX        =   2117
      _ExtentY        =   609
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "Tahoma"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Caption         =   "Check All"
   End
   Begin ITGControls.ITGCommandButton cmdUCAll 
      Height          =   345
      Left            =   1335
      TabIndex        =   7
      Top             =   4920
      Width           =   1200
      _ExtentX        =   2117
      _ExtentY        =   609
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "Tahoma"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Caption         =   "Uncheck All"
   End
End
Attribute VB_Name = "frmARDRList"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'IT Group Inc. 2005.09.23

Option Explicit

'Reference type
Enum eDRType
    SO
    SampleDemo
    BorrowerSlip
    Production
    Replacement
    Transfer
    Consignment
    ReturnPurchase
    Others
    MFGProductionOrder
    MFGMRIS
    APSOForBuyOut
    APPOForBuyOut 'Production order for buy-out
    APPRForPO
    APPRForPO_Canvass 'with canvass
    WRRPurchase
    WRRProduction
    WRRProductionReturn  'Return From Production
    WRRSalesReturn
    WRRSample
    WRRDemo
    WRRConsignment  'For Consignment
    WRRTransfer  'From DR Transfer
    WRRForReplacement
    WRRNonTrade     'For All Non Trade PO`s
    PurInvoice
    Project_Items  'Project Setup Item list
    PULLOUT  'Return Sales using Pullout module
    WOForRequest    'Work Order Parts for Request
    RequestForIssuance  'Request Slip for Supply Issuance
    RequestForPR  'Request Slip for Purchase Request
    EMPLOYEE
    Corporate
    Vans
    Supplier
    Exhibit
End Enum

Public mDRType As eDRType
Public mRefPK As String


Private connList As New ADODB.Connection

Private Sub cmdCAll_Click()
    If lvwList.ListItems.Count = 0 Then Exit Sub
    For i = 1 To lvwList.ListItems.Count
        lvwList.ListItems(i).Checked = True
    Next i
End Sub

Private Sub cmdCancel_Click()
    Unload Me
End Sub

Private Sub cmdOK_Click()
    Select Case mDRType
        Case APPOForBuyOut
'            frmAPPurchaseRequest.AddDetailsFromList
        Case WRRSalesReturn
            If lPullOutSlip Then
                FrmName.AddDetailsFromList_PullOut
            Else
                FrmName.AddDetailsFromList
            End If
        Case PULLOUT
            FrmName.AddDetailsFromList
        Case WRRTransfer, WRRConsignment, WRRDemo, WRRSample, WRRForReplacement, _
                WRRProductionReturn, WRRProduction
            If cString = "NOCOST" Then
            Else
                FrmName.AddDetailsFromList
            End If
        Case Else
            FrmName.AddDetailsFromList
    End Select
    Unload Me
End Sub

Private Sub cmdUCAll_Click()
    If lvwList.ListItems.Count = 0 Then Exit Sub
    For i = 1 To lvwList.ListItems.Count
        lvwList.ListItems(i).Checked = False
    Next i
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyEscape Then
        Unload Me
    End If
End Sub

Private Sub Form_Load()
    
    If lPullOutSlip Then
        If mDRType = WRRSalesReturn Then
            lvwList.Checkboxes = False
            cmdCAll.Visible = False
            cmdUCAll.Visible = False
            Set itmX = lvwList.ColumnHeaders.Add(, , "Invoice No.")
            lvwList.ColumnHeaders(9).Width = "1500"
        Else
            lvwList.Checkboxes = True
            cmdCAll.Visible = True
            cmdUCAll.Visible = True
        End If
    Else
        lvwList.Checkboxes = True
        cmdCAll.Visible = True
        cmdUCAll.Visible = True
    End If
    
    Select Case mDRType
        Case SO
            Reference "cSONo", "V_SOForDR", "cCode = '" & RepName & "' AND cType = 'Regular'"
            Caption = "DR Detail List [Reference: Sales Order - Sales]"
        Case EMPLOYEE
            Reference "cSONo", "V_SOForDR", "cCode = '" & RepName & "' AND cType = 'Employee'"
            Caption = "DR Detail List [Reference: Sales Order - Employee]"
        Case Corporate
            Reference "cSONo", "V_SOForDR", "cCode = '" & RepName & "' AND cType = 'Corporate'"
            Caption = "DR Detail List [Reference: Sales Order - Corporate]"
        Case Vans
            Reference "cSONo", "V_SOForDR", "cCode = '" & RepName & "' AND cType = 'Vans'"
            Caption = "DR Detail List [Reference: Sales Order - Vans]"
        Case Supplier
            Reference "cSONo", "V_SOForDR", "cCode = '" & RepName & "' AND cType = 'Supplier'"
            Caption = "DR Detail List [Reference: Sales Order - Supplier]"
        Case Exhibit
            Reference "cSONo", "V_SOForDR", "cCode = '" & RepName & "' AND cType = 'Exhibit'"
            Caption = "DR Detail List [Reference: Sales Order - Exhibit]"
        Case SampleDemo
            Reference "cSONo", "V_SOForDR", "cCode = '" & RepName & "' AND cType = 'Demo/Sample'"
            Caption = "DR Detail List [Reference: Sales Order - Sample/Demo]"
        Case BorrowerSlip
            Reference "cTranNo", "SBS", "cCode = '" & RepName & "'"
            Caption = "SBS Detail List [Reference: Supply Borrower Slip]"
        Case Consignment
            Reference "cSONo", "V_SOForDR", "cCode = '" & RepName & "' AND cType = 'For Consignment'"
            Caption = "DR Detail List [Reference: Sales Order - Consignment]"
        Case Production
            Reference "cTranNo", "V_ProdForDR"
            Caption = "DR Detail List [Reference: Production Order]"
            txtCode.Label = "Type"
            txtAddress.Label = "Remarks"
            txtName.Visible = False
        Case ReturnPurchase
            Reference "cWRRNo", "WRR", "cCode = '" & RepName & "' AND cType = 'Purchase' AND lCancelled = 0"
            Caption = "DR Listing [Reference: Purchase Return]"
        Case Replacement
            Reference "cWRRNo", "V_WRRReplacementForDR", "cCode = '" & RepName & "' AND cType = 'For Replacement'"
            Caption = "DR Listing [Reference: WRR For Replacement]"
        Case Transfer
            Reference "'Transfer'", "DR"
            Caption = "DR Listing [Reference: Transfer]"
            txtName.Visible = False
            txtCode.Visible = False
            txtAddress.Visible = False
            dtbDate.Visible = False
            lblDate.Visible = False
        Case Others
        Case MFGProductionOrder
            Reference "cSONo", "V_MFGSOForProduction"
            Caption = "Sales Order for Production List [Reference: Sales Order]"
        Case MFGMRIS
            Reference "cTranNo", "PRODUCTION", "lServed = 0 AND lCancelled = 0"
            Caption = "MRIS [Reference: Production Order]"
            txtCode.Label = "Type"
            txtAddress.Label = "Remarks"
            txtName.Visible = False
        Case WOForRequest
            Reference "cTranNo", "V_WOForRequest", "cType = '" & cString & "'"
            Caption = "Work Order Parts for Request [Reference: Work Order for " & cString & "]"
        Case RequestForIssuance
            Reference "cTranNo", "V_RequestForIssuance"
            Caption = "Request Slip for Supply Issuance [Reference: Request Slip]"
        Case RequestForPR
            Reference "cTranNo", "V_RequestForPR"
            Caption = "Request Slip for Purchase Request [Reference: Request Slip]"
        Case APSOForBuyOut
            Reference "cSONo", "V_SOForBuyOut"
            Caption = "Sales Order for Buy-Out List [Reference: Sales Order]"
        Case APPOForBuyOut
            Reference "cTranNo", "V_POForBuyOut"
            Caption = "Production Order for Buy-Out List [Reference: Production Order]"
            txtCode.Label = "Type"
            txtAddress.Label = "Remarks"
            txtName.Visible = False
        Case APPRForPO
            Reference "cPRNo", "V_PRForPO"
            Caption = "Purchase Request List [Reference: Purchase Request]"
            txtCode.Label = "Type"
            txtAddress.Label = "Requested"
            txtName.Visible = False
        Case APPRForPO_Canvass
            Reference "cPRNo", "V_PRForPO"
            Caption = "Purchase Request List [Reference: Purchase Request with Canvass]"
            txtCode.Label = "Type"
            txtAddress.Label = "Requested"
            txtName.Visible = False
        Case WRRNonTrade
            Reference "cPONo", "V_POForRRNonTrade", "cCode = '" & RepName & "'"
            Caption = "WRR Listing [Reference: Purchase Order]"
            txtCode.Label = "Supplier"
        Case WRRPurchase
            Reference "cPONo", "V_POForRR", "cCode = '" & RepName & "'"
            Caption = "WRR Listing [Reference: Purchase Order]"
            txtCode.Label = "Supplier"
        Case WRRProduction
            Reference "cTranNo", "V_ProdOrderForRR"
            Caption = "WRR Listing [Reference: Production Order]"
            txtCode.Label = "Type"
            txtAddress.Label = "Remarks"
            txtName.Visible = False
        Case WRRProductionReturn
            Reference "cTranNo", "PRODUCTION", "lCancelled = 0"
            Caption = "WRR Listing [Reference: Production Order (Bill of Materials)]"
            txtCode.Label = "Type"
            txtAddress.Label = "Remarks"
            txtName.Visible = False
        Case WRRSalesReturn
            If lPullOutSlip Then
                Reference "cPullOutNo", "PULLOUT", "cCode = '" & RepName & "' " & _
                        "AND lCancelled = 0 AND cPullOutNo NOT IN (SELECT cPullOutNo FROM WRR WHERE cCompanyID = '" & COID & "' AND lCancelled = 0 AND cPullOutNo IS NOT NULL)"
                Caption = "WRR Listing [Reference: Pull-Out Slip]"
            Else
                Reference "cInvNo", "SALES", "cCode = '" & RepName & "' AND cType <> 'Project' AND lCancelled = 0"
                Caption = "WRR Listing [Reference: Sales for Return]"
            End If
        Case PULLOUT
            Reference "cInvNo", "SALES", "cCode = '" & RepName & "' AND cType <> 'Project' AND lCancelled = 0"
            Caption = "Pull-Out Listing [Reference: Sales for Return]"
        Case WRRForReplacement
            Reference "cInvNo", "SALES", "cCode = '" & RepName & "' AND lCancelled = 0"
            Caption = "WRR Listing [Reference: Sales for Replacement]"
        Case WRRSample
            Reference "cDRNo", "V_DRForSI", "cDelCode = '" & RepName & "' AND cType = 'Sample/Demo'"
            Caption = "WRR Listing [Reference: DR Sample for Return]"
            txtCode.Label = "Client"
        Case WRRDemo
            Reference "cDRNo", "V_DRForSI", "cDelCode = '" & RepName & "' AND cType = 'Sample/Demo'"
            Caption = "WRR Listing [Reference: DR Demo for Return]"
            txtCode.Label = "Client"
        Case WRRConsignment
            Reference "cDRNo", "V_DRConsignmentForWRR", "cDelCode = '" & RepName & "' AND cType = 'Consignment'"
            Caption = "WRR Listing [Reference: DR For Consignment]"
            txtCode.Label = "Client"
        Case WRRTransfer
            Reference "cDRNo", "V_DRTransferForWRR", "cDelCode = '" & RepName & "' AND cType = 'Transfer'"
            Caption = "WRR Listing [Reference: DR For Transfer]"
            txtCode.Label = "Client"
        Case PurInvoice
            Reference "cWRRNo", "WRR", "cType = 'Purchase' AND lCancelled = 0"
            Caption = "Purchase Invoice Listing [Reference: Warehouse Receiving]"
            txtCode.Label = "Supplier"
        Case Project_Items
            Reference "cProjNo", "PROJECT", "cCode = '" & RepName & "' AND cProjNo = '" & cString & "' AND lCancelled = 0"
            Caption = "DR Listing [Reference: Project Setup]"
    End Select
    If lvwRef.ListItems.Count <> 0 Then lvwRef_ItemClick lvwRef.ListItems(lvwRef.SelectedItem.Index)
End Sub

'Reference list
Sub Reference(RefField As String, RefTable As String, Optional Condition As String)
    
    Set rs = New Recordset
    
    If Trim(Condition) <> "" Then
        Condition = "" & Condition
    End If
    
    'Select reference numbers
    If GetValueFrTable("lBit", "SYSTEM_OPTION", "cCode = 'ITM_VER_ALLSPLYREQ'") = True Then
        If GetValueFrTable("RoleID", "SEC_USER", "UserID = '" & sUserName & "'", True) = GetValueFrTable("cValue", "SYSTEM_OPTION", "cCode = 'ITM_VER_ALLSPLYREQ'") Then
            If Condition = "" Then
                Condition = ""
            Else
                Condition = " WHERE " & Condition
            End If
            sSQL = "SELECT DISTINCT " & Trim(RefField) & " AS cRefNo FROM " & Trim(RefTable) & Condition
        End If
    Else
        If Condition = "" Then
                Condition = ""
        Else
            Condition = " and " & Condition
        End If
        sSQL = "SELECT DISTINCT " & Trim(RefField) & " AS cRefNo FROM " & Trim(RefTable) & " WHERE cCompanyID = '" & COID & "'" & Condition
    End If
    OpenNewConnection connList
    rs.Open sSQL, connList, adOpenForwardOnly, adLockReadOnly
    
    lvwRef.ListItems.Clear
    Do Until rs.EOF
        Set itmX = lvwRef.ListItems.Add(, , Trim(rs!cRefNo))
    rs.MoveNext
    Loop
    
    Set rs = Nothing

End Sub

'Sets the SQL Query for DR item details
Sub ListQuery(RefPK As String)
Dim sCondition As String
If GetValueFrTable("lBit", "SYSTEM_OPTION", "cCode = 'ITM_VER_ALLSPLYREQ'") = True Then
    If GetValueFrTable("RoleID", "SEC_USER", "UserID = '" & sUserName & "'", True) = GetValueFrTable("cValue", "SYSTEM_OPTION", "cCode = 'ITM_VER_ALLSPLYREQ'") Then
        sCondition = ""
    Else
        sCondition = " AND A.cCompanyID = '" & COID & "'"
    End If
End If
    Select Case mDRType
        Case SO, Exhibit
            sSQL = "SELECT A.cCode AS RefCode, B.cName AS RefName, A.cAddress AS RefAddress, " & _
                    "dDate AS RefDate FROM SO A LEFT OUTER JOIN CLIENT_CUSTOMER B " & _
                    "ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
                    "WHERE A.cSONo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT SO_T.cItemNo, cDesc, cWH, cUnit, (nQty - ISNULL(nDelivered, 0)) AS nQty, nNetPrice AS nPrice, ((nQty - ISNULL(nDelivered, 0)) * nNetPrice) AS nAmount, nIdentity, nFactor FROM SO_T " & _
                    "LEFT OUTER JOIN V_DeliveredSOItem A ON SO_T.nIdentity = A.nRefIdentity AND SO_T.cItemNo = A.cItemNo " & _
                    "WHERE SO_T.cSONo = '" & Trim(RefPK) & "' AND SO_T.cCompanyID = '" & COID & "' AND lDRReady = 1 " & _
                    "AND (nQty - ISNULL(nDelivered, 0)) > 0"
            ListDetail
        Case EMPLOYEE, Corporate
            sSQL = "SELECT A.cCode AS RefCode, B.cName AS RefName, A.cAddress AS RefAddress, " & _
                    "dDate AS RefDate FROM SO A LEFT OUTER JOIN CLIENT_CUSTOMER B " & _
                    "ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
                    "WHERE A.cSONo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT SO_T.cItemNo, cDesc, cWH, cUnit, (nQty - ISNULL(nDelivered, 0)) AS nQty, nNetPrice AS nPrice, ((nQty - ISNULL(nDelivered, 0)) * nNetPrice) AS nAmount, nIdentity, nFactor FROM SO_T " & _
                    "LEFT OUTER JOIN V_DeliveredSOItem A ON SO_T.nIdentity = A.nRefIdentity AND SO_T.cItemNo = A.cItemNo " & _
                    "WHERE SO_T.cSONo = '" & Trim(RefPK) & "' AND SO_T.cCompanyID = '" & COID & "' AND lDRReady = 1 " & _
                    "AND (nQty - ISNULL(nDelivered, 0)) > 0"
            ListDetail
        Case Vans
            sSQL = "SELECT A.cCode AS RefCode, B.cName AS RefName, A.cAddress AS RefAddress, " & _
                    "dDate AS RefDate FROM SO A LEFT OUTER JOIN CLIENT_CUSTOMER B " & _
                    "ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
                    "WHERE A.cSONo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT SO_T.cItemNo, cDesc, cWH, cUnit, (nQty - ISNULL(nDelivered, 0)) AS nQty, nNetPrice AS nPrice, ((nQty - ISNULL(nDelivered, 0)) * nNetPrice) AS nAmount, nIdentity, nFactor FROM SO_T " & _
                    "LEFT OUTER JOIN V_DeliveredSOItem A ON SO_T.nIdentity = A.nRefIdentity AND SO_T.cItemNo = A.cItemNo " & _
                    "WHERE SO_T.cSONo = '" & Trim(RefPK) & "' AND SO_T.cCompanyID = '" & COID & "' AND lDRReady = 1 " & _
                    "AND (nQty - ISNULL(nDelivered, 0)) > 0"
            ListDetail
        Case SampleDemo
            sSQL = "SELECT A.cCode AS RefCode, B.cName AS RefName, A.cAddress AS RefAddress, " & _
                    "dDate AS RefDate FROM SO A LEFT OUTER JOIN  CLIENT_CUSTOMER  B " & _
                    "ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
                    "WHERE A.cSONo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT SO_T.cItemNo, cDesc, cWH, cUnit, (nQty - ISNULL(nDelivered, 0)) AS nQty, nNetPrice AS nPrice, ((nQty - ISNULL(nDelivered, 0)) * nNetPrice) AS nAmount, nIdentity, nFactor FROM SO_T " & _
                    "LEFT OUTER JOIN V_DeliveredSOItem A ON SO_T.nIdentity = A.nRefIdentity AND SO_T.cItemNo = A.cItemNo " & _
                    "WHERE SO_T.cSONo = '" & Trim(RefPK) & "' AND SO_T.cCompanyID = '" & COID & "' AND lDRReady = 1 " & _
                    "AND (nQty - ISNULL(nDelivered, 0)) > 0"
            ListDetail
        Case BorrowerSlip
            sSQL = "SELECT A.cCode AS RefCode, B.cName AS RefName, B.cAddress AS RefAddress, " & _
                    "dDate AS RefDate FROM SBS A LEFT OUTER JOIN  CLIENT_CUSTOMER  B " & _
                    "ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
                    "WHERE A.cTranNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT cItemNo, cDesc, ' ' as cWH, cUnit, nQty, 0 AS nPrice, 0 AS nAmount, '' as nIdentity, '' as nFactor FROM SBS_T " & _
                    "WHERE cTranNo = '" & Trim(RefPK) & "' AND cCompanyID = '" & COID & "'"
            ListDetail
        Case Consignment
            sSQL = "SELECT A.cCode AS RefCode, B.cName AS RefName, A.cAddress AS RefAddress, " & _
                    "dDate AS RefDate FROM SO A LEFT OUTER JOIN CLIENT_CUSTOMER  B " & _
                    "ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
                    "WHERE A.cSONo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT SO_T.cItemNo, cDesc, cWH, cUnit, (nQty - ISNULL(nDelivered, 0)) AS nQty, nNetPrice AS nPrice, ((nQty - ISNULL(nDelivered, 0)) * nNetPrice) AS nAmount, nIdentity, nFactor FROM SO_T " & _
                    "LEFT OUTER JOIN V_DeliveredSOItem A ON SO_T.nIdentity = A.nRefIdentity AND SO_T.cItemNo = A.cItemNo " & _
                    "WHERE SO_T.cSONo = '" & Trim(RefPK) & "' AND SO_T.cCompanyID = '" & COID & "' AND lDRReady = 1 " & _
                    "AND (nQty - ISNULL(nDelivered, 0)) > 0"
            ListDetail
        Case Production
            sSQL = "SELECT cType AS RefCode, '' AS RefName, ISNULL(cRemarks, '') AS RefAddress, " & _
                    "dDate AS RefDate FROM PRODUCTION A WHERE A.cTranNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT A.cItemNo, A.cDesc, C.cWH, A.cUnit, (A.nQty - ISNULL(nDelivered, 0)) AS nQty, A.nSRPrice AS nPrice, ((nQty - ISNULL(nDelivered, 0)) * A.nSRPrice) AS nAmount, nIdentity, nFactor FROM PRODUCTION_BM A " & _
                    "LEFT OUTER JOIN V_DeliveredProdItem B ON A.nIdentity = B.nRefIdentity AND A.cItemNo = B.cItemNo " & _
                    "LEFT OUTER JOIN ITEM C ON A.cItemNo = C.cItemNo AND A.cCompanyID = C.cCompanyID " & _
                    "WHERE A.cTranNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "' AND lDRReady = 1 " & _
                    "AND (nQty - ISNULL(nDelivered, 0)) > 0"
            ListDetail
        Case ReturnPurchase
            sSQL = "SELECT A.cCode AS RefCode, B.cName AS RefName, (B.cAddress + ', ' + B.cCity + ', ' + B.cCountry) AS RefAddress, " & _
                    "A.dDate AS RefDate FROM WRR A LEFT OUTER JOIN CLIENT_SUPPLIER B " & _
                    "ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
                    "WHERE A.cWRRNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT cItemNo, cDesc, cWH, cUnit, nQty, nPrice, nAmount, nIdentity, nFactor FROM WRR_T " & _
                    "WHERE cWRRNo = '" & Trim(RefPK) & "' AND cCompanyID = '" & COID & "'"
            ListDetail
        Case Replacement
            sSQL = "SELECT A.cCode AS RefCode, B.cName AS RefName, (B.cAddress + ', ' + B.cCity + ', ' + B.cCountry) AS RefAddress, " & _
                    "A.dDate AS RefDate FROM WRR A LEFT OUTER JOIN CLIENT_CUSTOMER  B " & _
                    "ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
                    "WHERE A.cWRRNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT cItemNo, cDesc, cWH, cUnit, nQty, nPrice, nAmount, nIdentity, nFactor FROM WRR_T " & _
                    "WHERE cWRRNo = '" & Trim(RefPK) & "' AND cCompanyID = '" & COID & "'"
            ListDetail
        Case Transfer
            sSQL = "SELECT cItemNo, cDesc, cWH, cUnit, 0 AS nQty, nSRPrice AS nPrice, 0 AS nAmount, 0 AS nIdentity, 1 as nFactor FROM ITEM " & _
                    "WHERE cCompanyID = '" & COID & "'"
            ListDetail
        Case MFGProductionOrder
            sSQL = "SELECT A.cCode AS RefCode, B.cName AS RefName, A.cAddress AS RefAddress, " & _
                    "dDate AS RefDate FROM SO A LEFT OUTER JOIN CLIENT_CUSTOMER  B " & _
                    "ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
                    "WHERE A.cSONo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "(SELECT SO_T.cItemNo, cDesc, cWH, cUnit, (nQty - ISNULL(nPO, 0)) AS nQty, nNetPrice AS nPrice, ((nQty - ISNULL(nPO, 0)) * nNetPrice) AS nAmount, nIdentity, nFactor FROM SO_T " & _
                    "LEFT OUTER JOIN V_ProdOrderedSOItem A ON SO_T.nIdentity = A.nRefIdentity AND SO_T.cItemNo = A.cItemNo " & _
                    "WHERE SO_T.cSONo = '" & Trim(RefPK) & "' AND SO_T.cCompanyID = '" & COID & "' AND cStatus = 'Production' " & _
                    "AND (nQty - ISNULL(nPO, 0)) > 0)" & _
                    "union all" & _
                    "(SELECT SO_T.cItemNo, cDesc, cWH, cUnit, (nQty - ISNULL(nPO, 0)) AS nQty, nNetPrice AS nPrice, ((nQty - ISNULL(nPO, 0)) * nNetPrice) AS nAmount, nIdentity, nFactor FROM SO_T " & _
                    "LEFT OUTER JOIN V_ProdOrderedSOItem A ON SO_T.nIdentity = A.nRefIdentity AND SO_T.cItemNo = A.cItemNo " & _
                    "WHERE SO_T.cSONo = '" & Trim(RefPK) & "' AND SO_T.cCompanyID = '" & COID & "' AND cStatus = 'Labor' " & _
                    "AND (nQty - ISNULL(nPO, 0)) > 0)" & _
                    "order by so_t.nIdentity asc"
            ListDetail
        Case MFGMRIS
            sSQL = "SELECT cType AS RefCode, '' AS RefName, ISNULL(cRemarks, '') AS RefAddress, " & _
                    "dDate AS RefDate FROM PRODUCTION A WHERE A.cTranNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT A.cItemNo, A.cDesc, B.cWH, A.cUnit, A.nQty, B.nSRPrice AS nPrice, (A.nQty * B.nSRPrice) AS nAmount, A.nIdentity, nFactor " & _
                    "FROM PRODUCTION_BM A LEFT OUTER JOIN ITEM B ON A.cItemNo = B.cItemNo AND A.cCompanyID = B.cCompanyID " & _
                    "WHERE A.cTranNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListDetail
        Case RequestForIssuance
            sSQL = "SELECT A.DeptID AS RefCode, B.Description AS RefName, '---' AS RefAddress, " & _
                    "A.dDate AS RefDate FROM REQUISITIONSLIP A " & _
                    "LEFT OUTER JOIN SEC_DEPARTMENT B ON A.DeptID = B.DeptID " & _
                    "WHERE A.cTranNo = '" & Trim(RefPK) & "'" & sCondition
            ListHeader
            sSQL = "SELECT B.cItemNo, B.cDesc, '---' AS cWH, B.cUnit, (B.nQty - ISNULL(A.nRequested, 0)) AS nQty, 0 AS nPrice, " & _
                    "0 AS nAmount, B.nIdentity,b.cBarcodeID " & _
                    "FROM REQUISITIONSLIP_T B " & _
                    "LEFT OUTER JOIN V_IssuedRequestedItem A ON B.nIdentity = A.nRefIdentity AND B.cItemNo = A.cItemNo " & _
                    "WHERE B.cTranNo = '" & Trim(RefPK) & "'" & sCondition & _
                    "AND (B.nQty - ISNULL(A.nRequested, 0)) > 0 AND B.cStatus <> 'No Stock' AND B.cType <> 'New' " & _
                    "UNION ALL " & _
                    "SELECT B.cItemNo, B.cDesc, '---' AS cWH, B.cUnit, (B.nQty - ISNULL(A.nRequested, 0)) AS nQty, 0 AS nPrice, " & _
                    "0 AS nAmount, B.nIdentity,b.cBarcodeID " & _
                    "FROM REQUISITIONSLIP_T B " & _
                    "LEFT OUTER JOIN V_IssuedRequestedItem A ON B.nIdentity = A.nRefIdentity AND B.cItemNo = A.cItemNo " & _
                    "WHERE B.cTranNo = '" & Trim(RefPK) & "' " & sCondition & _
                    "AND (B.nQty - ISNULL(A.nRequested, 0)) > 0 AND B.cStatus = 'No Stock' AND B.cType <> 'New' AND B.cRefWRRNo IS NOT NULL"
            ListDetail
        Case RequestForPR
            sSQL = "SELECT A.DeptID AS RefCode, B.Description AS RefName, '---' AS RefAddress, " & _
                    "A.dDate AS RefDate FROM REQUISITIONSLIP A " & _
                    "LEFT OUTER JOIN SEC_DEPARTMENT B ON A.DeptID = B.DeptID " & _
                    "WHERE A.cTranNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT B.cItemNo, B.cDesc, '---' AS cWH, B.cUnit, (B.nQty - ISNULL(A.nRequested, 0)) AS nQty, 0 AS nPrice, " & _
                    "0 AS nAmount, B.nIdentity, nFactor, b.cBarcodeID " & _
                    "FROM REQUISITIONSLIP_T B " & _
                    "LEFT OUTER JOIN V_IssuedRequestedItem A ON B.nIdentity = A.nRefIdentity AND B.cItemNo = A.cItemNo " & _
                    "WHERE B.cTranNo = '" & Trim(RefPK) & "' AND B.cCompanyID = '" & COID & "' " & _
                    "AND (B.nQty - ISNULL(A.nRequested, 0)) > 0 AND B.cStatus = 'No Stock' AND B.cType <> 'New' " & _
                    "AND B.cRefWRRNo IS NULL "
            ListDetail
        Case WOForRequest
            sSQL = "SELECT '---' AS RefCode, '---' AS RefName, '---' AS RefAddress, " & _
                    "A.dDate AS RefDate FROM WO A " & _
                    "WHERE A.cTranNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT B.cItemNo, B.cDesc, '---' AS cWH, B.cUnit, (B.nQty - ISNULL(A.nRequested, 0)) AS nQty, B.nCost AS nPrice, " & _
                    "((B.nQty - ISNULL(A.nRequested, 0)) * B.nCost) AS nAmount, B.nIdentity " & _
                    "FROM WO_PARTS B " & _
                    "LEFT OUTER JOIN V_RequestedWOItem A ON B.nIdentity = A.nRefIdentity AND B.cItemNo = A.cItemNo " & _
                    "WHERE B.cTranNo = '" & Trim(RefPK) & "' AND B.cCompanyID = '" & COID & "' " & _
                    "AND (B.nQty - ISNULL(A.nRequested, 0)) > 0"
            ListDetail
        Case APSOForBuyOut
            sSQL = "SELECT A.cCode AS RefCode, B.cName AS RefName, A.cAddress AS RefAddress, " & _
                    "dDate AS RefDate FROM SO A LEFT OUTER JOIN CLIENT_CUSTOMER  B " & _
                    "ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
                    "WHERE A.cSONo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT SO_T.cItemNo, cDesc, cWH, cUnit, (nQty - ISNULL(nRequested, 0)) AS nQty, nNetPrice AS nPrice, ((nQty - ISNULL(nRequested, 0)) * nNetPrice) AS nAmount, nIdentity, nFactor FROM SO_T " & _
                    "LEFT OUTER JOIN V_RequestedSOItem A ON SO_T.nIdentity = A.nRefIdentity AND SO_T.cItemNo = A.cItemNo " & _
                    "WHERE SO_T.cSONo = '" & Trim(RefPK) & "' AND SO_T.cCompanyID = '" & COID & "' AND (cStatus = 'Buy-out' OR cStatus = 'No Stock') " & _
                    "AND (nQty - ISNULL(nRequested, 0)) > 0"
            ListDetail
        Case APPOForBuyOut 'Production order for request
            sSQL = "SELECT cType AS RefCode, '' AS RefName, ISNULL(cRemarks, '') AS RefAddress, " & _
                    "dDate AS RefDate FROM PRODUCTION A WHERE A.cTranNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT A.cItemNo, cDesc, '' AS cWH, cUnit, (nQty - ISNULL(nRequested, 0)) AS nQty, " & _
                    "nSRPrice AS nPrice, ((ISNULL(nQty, 0)) * (ISNULL(nSRPrice, 0))) AS nAmount, nIdentity, nFactor " & _
                    "FROM PRODUCTION_BM A LEFT OUTER JOIN V_RequestedProdItem B ON A.nIdentity = B.nRefIdentity AND A.cItemNo = B.cItemNo " & _
                    "WHERE A.cTranNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "' AND (cStatus = 'Buy-out' OR cStatus = 'No Stock') " & _
                    "AND (nQty - ISNULL(nRequested, 0)) > 0"
            ListDetail
        Case APPRForPO
            sSQL = "SELECT cType AS RefCode, '' AS RefName, (ISNULL(cRequestedBy, '') + ', ' + ISNULL(cDepartment, '')) AS RefAddress, " & _
                    "dDate AS RefDate FROM REQUISITION A WHERE A.cPRNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT REQUISITION_T.cItemNo, cDesc,'' AS cWH, cUnit, (nQty - ISNULL(nOrdered,0)) AS nQty, 0 AS nPrice, 0 AS nAmount, nIdentity, nFactor FROM REQUISITION_T " & _
                    "LEFT OUTER JOIN V_OrderedRequest A ON REQUISITION_T.nIdentity = A.nPRIdentity AND REQUISITION_T.cItemNo = A.cItemNo " & _
                    "WHERE REQUISITION_T.cPRNo = '" & Trim(RefPK) & "' AND REQUISITION_T.cCompanyID = '" & COID & "' AND lServed = 0 " & _
                    "AND (nQty - ISNULL(nOrdered,0)) > 0"
            ListDetail
        Case APPRForPO_Canvass
            sSQL = "SELECT cType AS RefCode, '' AS RefName, (ISNULL(cRequestedBy, '') + ', ' + ISNULL(cDepartment, '')) AS RefAddress, " & _
                    "dDate AS RefDate FROM REQUISITION A WHERE A.cPRNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT Z.cItemNo, Z.cDesc,'' AS cWH, Z.cUnit, (Z.nQty - ISNULL(A.nOrdered,0)) AS nQty, " & _
                    "c.nPrice, (Z.nQty - ISNULL(A.nOrdered,0)) * c.nPrice AS nAmount, Z.nIdentity, nFactor " & _
                    "FROM REQUISITION_T Z " & _
                    "LEFT OUTER JOIN V_OrderedRequest A ON Z.nIdentity = A.nPRIdentity " & _
                        "AND Z.cItemNo = A.cItemNo " & _
                    "LEFT OUTER JOIN CANVASS B ON Z.cCompanyID = B.cCompanyID " & _
                        "AND Z.cPRNo = B.cPRNo " & _
                    "LEFT OUTER JOIN CANVASS_T C ON B.cCompanyID = C.cCompanyID " & _
                        "AND B.cTranNo = C.cTranNo " & _
                    "WHERE Z.cPRNo = '" & Trim(RefPK) & "' AND Z.cCompanyID = '" & COID & "' " & _
                    "AND Z.lServed = 0 AND (Z.nQty - ISNULL(A.nOrdered,0)) > 0 " & _
                    "AND B.cItemNo = z.citemNo " & _
                    "AND C.lApproved = 1 AND C.cCode = '" & Trim(RepName) & "'"
            ListDetail
'        Case WRRNonTrade
'            sSQL = "SELECT A.cCode AS RefCode, B.cName AS RefName, A.cAddress AS RefAddress, " & _
'                    "dDate AS RefDate FROM PO A LEFT OUTER JOIN CLIENT_SUPPLIER B " & _
'                    "ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
'                    "WHERE A.cPONo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
'            ListHeader
'            sSQL = "SELECT PO_T.cItemNo, cDesc, '' AS cWH, cUnit, (nQty - ISNULL(nReceived,0)) AS nQty, nPrice, ((nQty - ISNULL(nReceived,0)) * nPrice) AS nAmount, nIdentity, nFactor FROM PO_T " & _
'                    "LEFT OUTER JOIN V_ReceivedOrder A ON PO_T.nIdentity = A.nRefIdentity AND PO_T.cItemNo = A.cItemNo " & _
'                    "WHERE PO_T.cPONo = '" & Trim(RefPK) & "' AND PO_T.cCompanyID = '" & COID & "' " & _
'                    "AND (nQty - ISNULL(nReceived,0)) > 0"
'            ListDetail
        Case WRRPurchase, WRRNonTrade
            sSQL = "SELECT A.cCode AS RefCode, B.cName AS RefName, A.cAddress AS RefAddress, " & _
                    "dDate AS RefDate FROM PO A LEFT OUTER JOIN CLIENT_SUPPLIER B " & _
                    "ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
                    "WHERE A.cPONo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT PO_T.cItemNo, cDesc, '' AS cWH, cUnit, (nQty - ISNULL(nReceived,0)) AS nQty, nPrice, ((nQty - ISNULL(nReceived,0)) * nPrice) AS nAmount, nIdentity, nFactor FROM PO_T " & _
                    "LEFT OUTER JOIN V_ReceivedOrder A ON PO_T.nIdentity = A.nRefIdentity AND PO_T.cItemNo = A.cItemNo " & _
                    "WHERE PO_T.cPONo = '" & Trim(RefPK) & "' AND PO_T.cCompanyID = '" & COID & "' " & _
                    "AND (nQty - ISNULL(nReceived,0)) > 0"
            ListDetail
        Case WRRProduction
            sSQL = "SELECT cType AS RefCode, '' AS RefName, ISNULL(cRemarks, '') AS RefAddress, " & _
                    "dDate AS RefDate FROM PRODUCTION A WHERE A.cTranNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT A.cItemNo, A.cDesc, B.cWH, A.cUnit, (A.nQty - ISNULL(nReceived,0)) AS nQty, B.nSRPrice AS nPrice, ((A.nQty - ISNULL(nReceived,0)) * B.nSRPrice) AS nAmount, A.nIdentity, nFactor " & _
                    "FROM PRODUCTION_FG A LEFT OUTER JOIN ITEM B ON A.cItemNo = B.cItemNo AND A.cCompanyID = B.cCompanyID " & _
                    "LEFT OUTER JOIN V_ReceivedProdOrder C ON A.nIdentity = C.nRefIdentity AND A.cItemNo = C.cItemNo " & _
                    "WHERE A.cTranNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "' " & _
                    "AND (A.nQty - ISNULL(nReceived,0)) > 0"
            ListDetail
        Case WRRProductionReturn
            sSQL = "SELECT cType AS RefCode, '' AS RefName, ISNULL(cRemarks, '') AS RefAddress, " & _
                    "dDate AS RefDate FROM PRODUCTION A WHERE A.cTranNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT A.cItemNo, A.cDesc, B.cWH, A.cUnit, A.nQty, B.nSRPrice AS nPrice, (A.nQty * B.nSRPrice) AS nAmount, A.nIdentity, nFactor " & _
                    "FROM PRODUCTION_BM A LEFT OUTER JOIN ITEM B ON A.cItemNo = B.cItemNo AND A.cCompanyID = B.cCompanyID " & _
                    "WHERE A.cTranNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListDetail
        Case WRRSalesReturn
            If lPullOutSlip Then
                sSQL = "SELECT A.cCode AS RefCode, B.cName AS RefName, (B.cAddress + ', ' + B.cCity + ', ' + B.cCountry) AS RefAddress, " & _
                        "A.dDate AS RefDate FROM PULLOUT A LEFT OUTER JOIN CLIENT_CUSTOMER B " & _
                        "ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
                        "WHERE A.cPullOutNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
                ListHeader
                sSQL = "SELECT A.cRefNo AS cInvNo, A.cItemNo, A.cDesc, A.cWH, A.cUnit, A.nQty, A.nPrice, A.nAmount, A.nRefIdentity AS nIdentity, nFactor FROM PULLOUT_T A " & _
                        "WHERE cPullOutNo = '" & Trim(RefPK) & "' AND cCompanyID = '" & COID & "'"
                ListDetail
            Else
                sSQL = "SELECT A.cCode AS RefCode, B.cName AS RefName, (B.cAddress + ', ' + B.cCity + ', ' + B.cCountry) AS RefAddress, " & _
                        "A.dDate AS RefDate FROM SALES A LEFT OUTER JOIN CLIENT_CUSTOMER B " & _
                        "ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
                        "WHERE A.cInvNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
                ListHeader
                sSQL = "SELECT cItemNo, cDesc, cWH, cUnit, nQty, nPrice, nAmount, nIdentity, cPricing, nFactor FROM SALES_T " & _
                        "WHERE cInvNo = '" & Trim(RefPK) & "' AND cCompanyID = '" & COID & "'"
                ListDetail
            End If
        Case PULLOUT
            sSQL = "SELECT A.cCode AS RefCode, B.cName AS RefName, (B.cAddress + ', ' + B.cCity + ', ' + B.cCountry) AS RefAddress, " & _
                    "A.dDate AS RefDate FROM SALES A LEFT OUTER JOIN CLIENT_CUSTOMER B " & _
                    "ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
                    "WHERE A.cInvNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT cItemNo, cDesc, cWH, cUnit, nQty, nPrice, nAmount, nIdentity, cPricing, nFactor FROM SALES_T " & _
                    "WHERE cInvNo = '" & Trim(RefPK) & "' AND cCompanyID = '" & COID & "'"
            ListDetail
        Case WRRForReplacement
            sSQL = "SELECT A.cCode AS RefCode, B.cName AS RefName, (B.cAddress + ', ' + B.cCity + ', ' + B.cCountry) AS RefAddress, " & _
                    "A.dDate AS RefDate FROM SALES A LEFT OUTER JOIN CLIENT_CUSTOMER B " & _
                    "ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
                    "WHERE A.cInvNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT cItemNo, cDesc, cWH, cUnit, nQty, nPrice, nAmount, nIdentity, nFactor FROM SALES_T " & _
                    "WHERE cInvNo = '" & Trim(RefPK) & "' AND cCompanyID = '" & COID & "'"
            ListDetail
        Case WRRSample
            sSQL = "SELECT A.cDelCode AS RefCode, B.cName AS RefName, (B.cAddress + ', ' + B.cCity + ', ' + B.cCountry) AS RefAddress, " & _
                    "A.dDate AS RefDate FROM DR A LEFT OUTER JOIN CLIENT_CUSTOMER B " & _
                    "ON A.cDelCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
                    "WHERE A.cDRNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'" ' AND A.cType = 'Sample'"
            ListHeader
            sSQL = "SELECT DR_T.cItemNo, cDesc, cWH, cUnit, (nQty - ISNULL(nInvoiced, 0)) AS nQty, nPrice, ((nQty - ISNULL(nInvoiced, 0)) * nPrice) AS nAmount, nIdentity, nFactor FROM DR_T " & _
                    "LEFT OUTER JOIN V_InvoicedDRItem A ON DR_T.nIdentity = A.nRefIdentity AND DR_T.cItemNo = A.cItemNo " & _
                    "WHERE DR_T.cDRNo = '" & Trim(RefPK) & "' AND DR_T.cCompanyID = '" & COID & "'" & _
                    "AND (nQty - ISNULL(nInvoiced, 0)) > 0"
            ListDetail
        Case WRRDemo
            sSQL = "SELECT A.cDelCode AS RefCode, B.cName AS RefName, (B.cAddress + ', ' + B.cCity + ', ' + B.cCountry) AS RefAddress, " & _
                    "A.dDate AS RefDate FROM DR A LEFT OUTER JOIN CLIENT_CUSTOMER B " & _
                    "ON A.cDelCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
                    "WHERE A.cDRNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'" 'AND A.cType = 'Demo'"
            ListHeader
            sSQL = "SELECT DR_T.cItemNo, cDesc, cWH, cUnit, (nQty - ISNULL(nInvoiced, 0)) AS nQty, nPrice, ((nQty - ISNULL(nInvoiced, 0)) * nPrice) AS nAmount, nIdentity, nFactor FROM DR_T " & _
                    "LEFT OUTER JOIN V_InvoicedDRItem A ON DR_T.nIdentity = A.nRefIdentity AND DR_T.cItemNo = A.cItemNo " & _
                    "WHERE DR_T.cDRNo = '" & Trim(RefPK) & "' AND DR_T.cCompanyID = '" & COID & "'" & _
                    "AND (nQty - ISNULL(nInvoiced, 0)) > 0"
            ListDetail
        Case WRRConsignment
            sSQL = "SELECT A.cDelCode AS RefCode, B.cName AS RefName, (B.cAddress + ', ' + B.cCity + ', ' + B.cCountry) AS RefAddress, " & _
                    "A.dDate AS RefDate FROM DR A LEFT OUTER JOIN CLIENT_CUSTOMER B " & _
                    "ON A.cDelCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
                    "WHERE A.cDRNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'" 'AND A.cType = 'Demo'"
            ListHeader
            sSQL = "SELECT DR_T.cItemNo, cDesc, cWH, cUnit, (nQty - ISNULL(nInvoiced, 0)) AS nQty, nPrice, ((nQty - ISNULL(nInvoiced, 0)) * nPrice) AS nAmount, nIdentity, nFactor FROM DR_T " & _
                    "LEFT OUTER JOIN V_InvoicedDRItem A ON DR_T.nIdentity = A.nRefIdentity AND DR_T.cItemNo = A.cItemNo " & _
                    "WHERE DR_T.cDRNo = '" & Trim(RefPK) & "' AND DR_T.cCompanyID = '" & COID & "'" & _
                    "AND (nQty - ISNULL(nInvoiced, 0)) > 0"
            ListDetail
        Case WRRTransfer
            sSQL = "SELECT A.cDelCode AS RefCode, B.cName AS RefName, (B.cAddress + ', ' + B.cCity + ', ' + B.cCountry) AS RefAddress, " & _
                    "A.dDate AS RefDate FROM DR A LEFT OUTER JOIN CLIENT_CUSTOMER B " & _
                    "ON A.cDelCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
                    "WHERE A.cDRNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'" 'AND A.cType = 'Demo'"
            ListHeader
            sSQL = "SELECT DR_T.cItemNo, cDesc, cWH, cUnit, (nQty - ISNULL(nInvoiced, 0)) AS nQty, nPrice, ((nQty - ISNULL(nInvoiced, 0)) * nPrice) AS nAmount, nIdentity, nFactor FROM DR_T " & _
                    "LEFT OUTER JOIN V_InvoicedDRItem A ON DR_T.nIdentity = A.nRefIdentity AND DR_T.cItemNo = A.cItemNo " & _
                    "WHERE DR_T.cDRNo = '" & Trim(RefPK) & "' AND DR_T.cCompanyID = '" & COID & "'" & _
                    "AND (nQty - ISNULL(nInvoiced, 0)) > 0"
            ListDetail
        Case PurInvoice
            sSQL = "SELECT A.cCode AS RefCode, B.cName AS RefName, (B.cAddress + ', ' + B.cCity + ', ' + B.cCountry) AS RefAddress, " & _
                    "A.dDate AS RefDate FROM WRR A LEFT OUTER JOIN CLIENT_SUPPLIER B " & _
                    "ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
                    "WHERE A.cWRRNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
            ListHeader
            sSQL = "SELECT cItemNo, cDesc, cWH, cUnit, nQty, nPrice, nAmount, nIdentity, nFactor FROM WRR_T " & _
                    "WHERE cWRRNo = '" & Trim(RefPK) & "' AND cCompanyID = '" & COID & "'"
            ListDetail
        Case Project_Items
                sSQL = "SELECT A.cCode AS RefCode, B.cName AS RefName, (B.cAddress + ', ' + B.cCity + ', ' + B.cCountry) AS RefAddress, " & _
                        "A.dDate AS RefDate FROM PROJECT A LEFT OUTER JOIN CLIENT_CUSTOMER B " & _
                        "ON A.cCode = B.cCode AND A.cCompanyID = B.cCompanyID " & _
                        "WHERE A.cProjNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
                ListHeader
                sSQL = "SELECT A.cItemNo, A.cDesc, B.cWH, A.cUnit, A.nQty, A.nPrice, (A.nQty * A.nPrice) AS nAmount, A.nIdentity, nFactor FROM PROJECT_MC A " & _
                        "LEFT OUTER JOIN ITEM B ON A.cItemNo = B.cItemNo AND A.cCompanyID = B.cCompanyID " & _
                        "WHERE A.cProjNo = '" & Trim(RefPK) & "' AND A.cCompanyID = '" & COID & "'"
                ListDetail
        Case Else
            Exit Sub
        End Select

    If cString = "NOCOST" Then
        lvwList.ColumnHeaders(6).Width = "0"
        lvwList.ColumnHeaders(7).Width = "0"
    End If

End Sub

'Header details
Sub ListHeader()
    Set rs = New Recordset
    OpenNewConnection connList
    rs.Open sSQL, connList, adOpenForwardOnly, adLockReadOnly
    If rs.RecordCount = 0 Then
        Set rs = Nothing
        Exit Sub
    End If
    txtCode = rs!RefCode & ""
    txtName = rs!refname & ""
    txtAddress = rs!RefAddress & ""
    dtbDate.Text = Format(rs!RefDate, "MM/dd/yyyy")
    Set rs = Nothing
End Sub

'Item details
Sub ListDetail()
    
    Set rs = New Recordset
    DoEvents
    
    OpenNewConnection connList
    rs.Open sSQL, connList, adOpenForwardOnly, adLockReadOnly
    lvwList.ListItems.Clear
    lvwList.Visible = False
    lblFOC.Visible = False
    Do Until rs.EOF
        If lPullOutSlip Then
            If mDRType = WRRSalesReturn Then
                Set itmX = lvwList.ListItems.Add(, , Trim(rs!cItemNo) & "")
                itmX.SubItems(1) = Trim(rs!cDesc) & ""
                itmX.SubItems(2) = Trim(rs!cWH) & ""
                itmX.SubItems(3) = Trim(rs!cUnit) & ""
                itmX.SubItems(4) = Format(rs!nQty, "###,##0.#0") & ""
                itmX.SubItems(5) = Format(rs!nPrice, "###,##0.#0") & ""
                itmX.SubItems(6) = Format(rs!nAmount, "###,##0.#0") & ""
                itmX.SubItems(7) = rs!nIdentity & ""
                itmX.SubItems(8) = Trim(rs!cInvNo) & ""
            Else
                Set itmX = lvwList.ListItems.Add(, , Trim(rs!cItemNo) & "")
                itmX.SubItems(1) = Trim(rs!cDesc) & ""
                itmX.SubItems(2) = Trim(rs!cWH) & ""
                itmX.SubItems(3) = Trim(rs!cUnit) & ""
                itmX.SubItems(4) = Format(rs!nQty, "###,##0.#0") & ""
                itmX.SubItems(5) = Format(rs!nPrice, "###,##0.#0") & ""
                itmX.SubItems(6) = Format(rs!nAmount, "###,##0.#0") & ""
                itmX.SubItems(7) = rs!nIdentity & ""
                If (mDRType = WRRSalesReturn) Or (mDRType = PULLOUT) Then
                    If UCase(Trim(rs!cPricing)) = "FOC" Then
                        itmX.ForeColor = &HC0&
                        lvwList.ListItems.Item(lvwList.ListItems.Count).Bold = True
                        lblFOC.Visible = True
                    End If
                End If
            End If
        Else
            Set itmX = lvwList.ListItems.Add(, , Trim(rs!cItemNo) & "")
            itmX.SubItems(1) = Trim(rs!cDesc) & ""
            itmX.SubItems(2) = Trim(rs!cWH) & ""
            itmX.SubItems(3) = Trim(rs!cUnit) & ""
            itmX.SubItems(4) = Format(rs!nQty, "###,##0.#0") & ""
            itmX.SubItems(5) = Format(rs!nPrice, "###,##0.#0") & ""
            itmX.SubItems(6) = Format(rs!nAmount, "###,##0.#0") & ""
            itmX.SubItems(7) = rs!nIdentity & ""
            If mDRType <> WOForRequest And mDRType <> RequestForIssuance Then
                itmX.SubItems(8) = rs!nFactor & ""
            End If
            If (mDRType = WRRSalesReturn) Or (mDRType = PULLOUT) Then
                If UCase(Trim(rs!cPricing)) = "FOC" Then
                    itmX.ForeColor = &HC0&
                    lvwList.ListItems.Item(lvwList.ListItems.Count).Bold = True
                    lblFOC.Visible = True
                End If
            End If
        End If
        rs.MoveNext
    Loop
    
    OtherItem
    
    lvwList.Visible = True
    Set rs = Nothing
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If lPullOutSlip Then
        If mDRType = WRRSalesReturn Then
            lvwList.ColumnHeaders.Remove (8)
        End If
    End If
    RepName = ""
    cString = ""
    Set connList = Nothing
    Set frmARDRList = Nothing
End Sub

Private Sub lvwList_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    lvwList.Sorted = True
    lvwList.SortKey = ColumnHeader.Index - 1
End Sub

Private Sub lvwList_KeyPress(KeyAscii As Integer)
    If lvwList.ListItems.Count = 0 Then Exit Sub
    If KeyAscii = 13 Then
        If lvwList.SelectedItem.Checked = True Then
            lvwList.SelectedItem.Checked = False
        Else
            lvwList.SelectedItem.Checked = True
        End If
    End If
End Sub

Private Sub lvwRef_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    lvwRef.Sorted = True
    lvwRef.SortKey = ColumnHeader.Index - 1
End Sub

Private Sub lvwRef_ItemClick(ByVal Item As MSComctlLib.ListItem)
    If lvwRef.ListItems.Count = 0 Then Exit Sub
    mRefPK = Trim(lvwRef.SelectedItem)
    ListQuery (Trim(lvwRef.SelectedItem))
End Sub

Private Sub lvwRef_KeyPress(KeyAscii As Integer)
    If lvwRef.ListItems.Count = 0 Then Exit Sub
    If KeyAscii = 13 Then lvwRef_ItemClick lvwRef.ListItems(lvwRef.SelectedItem.Index)
End Sub

Sub OtherItem()
    Select Case mDRType
        Case WRRSalesReturn
            If lPullOutSlip Then Exit Sub
            Set itmX = lvwList.ListItems.Add(, , "Other Item")
            itmX.SubItems(1) = ""
            itmX.SubItems(2) = ""
            itmX.SubItems(3) = ""
            itmX.SubItems(4) = "0"
            itmX.SubItems(5) = "0"
            itmX.SubItems(6) = "0"
            itmX.SubItems(7) = "0"
        Case PULLOUT, APPRForPO_Canvass, WRRForReplacement, ReturnPurchase, WRRPurchase
            Set itmX = lvwList.ListItems.Add(, , "Other Item")
            itmX.SubItems(1) = ""
            itmX.SubItems(2) = ""
            itmX.SubItems(3) = ""
            itmX.SubItems(4) = "0"
            itmX.SubItems(5) = "0"
            itmX.SubItems(6) = "0"
            itmX.SubItems(7) = "0"
    End Select
End Sub



